package cn.xuewei.controller;

import cn.xuewei.mapper.RechargeMapper;
import cn.xuewei.pojo.dto.RechargeDTO;
import cn.xuewei.pojo.entity.Recharge;
import cn.xuewei.response.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/recharge/")
public class RechargeController {
    @Autowired
    private RechargeMapper rechargeMapper;
    @RequestMapping("insert")
    public ResultVO insert(@RequestBody RechargeDTO rechargeDTO){
        Recharge recharge = new Recharge();
        BeanUtils.copyProperties(rechargeDTO, recharge);
        recharge.setTime(new Date());
        recharge.setStatus("审核中");
        rechargeMapper.insert(recharge);
        return ResultVO.ok();
    }

    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @RequestMapping("admin")
    public ResultVO admin(){
        List<Recharge> list = rechargeMapper.selectForAdmin();
        return ResultVO.ok(list);
    }

    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @RequestMapping("{userid}/adminRecharge/{amount}/{id}")
    public ResultVO adminCharge(@PathVariable Integer userid,@PathVariable int amount,@PathVariable Integer id){
        rechargeMapper.update(userid,amount,id);
        return ResultVO.ok();

    }

}
